package it.univpm.diiga.semanticsearch.ricerca;
import it.univpm.diiga.semanticsearch.configurazione.Configuration;
import it.univpm.diiga.semanticsearch.gestione.XMLFileFilter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.SocketTimeoutException;
import java.util.Hashtable;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;


public class Settings extends JDialog {

	private JComboBox repoType;

	private JTextField proxy;
	private JTextField proxyPort;
	private JTextField path;
	final JCheckBox enableProxy;

	private Hashtable settings;

	/**
	 * Create the dialog
	 */
	public Settings() {
		super();
		setTitle("Settings");
		getContentPane().setLayout(null);
		setBounds(100, 100, 500, 425);

		final JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.setBounds(0, 0, 500, 322);
		getContentPane().add(tabbedPane);

	  	final JPanel localRepo = new JPanel();
	  	localRepo.setLayout(null);

	  	tabbedPane.addTab("Local Repository", null, localRepo, null);
		
		
	  	final JButton chooseButton = new JButton();
	  	chooseButton.addActionListener(new ActionListener() {
	  		public void actionPerformed(final ActionEvent e) {
	  				final JFileChooser fc = new JFileChooser();
	  				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fc.setFileFilter(new XMLFileFilter());	   
	  		        int returnVal = fc.showOpenDialog(Settings.this);

	  		        if (returnVal == JFileChooser.APPROVE_OPTION) {
	  		            File file = fc.getSelectedFile();
	  		            path.setText(file.getAbsolutePath());
	  		            /*try {
							Configuration.setNewConfiguration("repository", "link", file.getAbsolutePath());
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}*/
	  		        }
	  		   
	  		}
	  	});
	  	chooseButton.setText("Choose");
	  	chooseButton.setBounds(372, 63, 99, 25);
	  	localRepo.add(chooseButton);
		
	  	path = new JTextField();
	  	path.setEditable(false);
	  	path.setBounds(168, 63, 198, 25);
	  	localRepo.add(path);

	  	final JLabel localRepositoryLabel = new JLabel();
	  	localRepositoryLabel.setText("Local Repository:");
	  	localRepositoryLabel.setBounds(45, 73, 111, 15);
	  	localRepo.add(localRepositoryLabel);
		
		
	  	repoType = new JComboBox(new String[]{"XML","mySql"});
	  	repoType.setSelectedIndex(0);
	  	repoType.setBounds(168, 114, 83, 24);
	  	repoType.setEnabled(false);
	  	
	  	localRepo.add(repoType);

	  	final JLabel formatLabel = new JLabel();
	  	formatLabel.setText("Format:");
	  	formatLabel.setBounds(88, 123, 68, 15);
	  	localRepo.add(formatLabel);

		
	  	final JPanel proxyPanel = new JPanel();
	  	proxyPanel.setLayout(null);
		//final JPanel popup = new JPanel();
	  	//proxyPanel.setBounds(314, 99, 233, 220);
		//getContentPane().add(popup);
	  	tabbedPane.addTab("Connection", null, proxyPanel, null);
	  	

	  	proxy = new JTextField();
	  	proxy.setBounds(91, 45, 188, 19);
	  	proxyPanel.add(proxy);
		
		
		

	  	proxyPort = new JTextField();
	  	proxyPort.setBounds(91, 83, 188, 19);
	  	proxyPanel.add(proxyPort);
		//proxyPanel.setResizable(false);
		//proxyPanel.setModal(true);
		
	  	enableProxy=new JCheckBox();
	  	enableProxy.setBounds(91, 124, 188, 19);
	  	enableProxy.setText("Enable proxy");
	  	enableProxy.addActionListener(new ActionListener() {
	  		public void actionPerformed(final ActionEvent e) {
	  				proxy.setEnabled(enableProxy.isSelected());
	  				proxyPort.setEnabled(enableProxy.isSelected());
	  			
	  		}
	  		
	  		
	  	});
		
		
	  	proxy.setEnabled(enableProxy.isSelected());
	  	proxyPort.setEnabled(enableProxy.isSelected());
	  	proxyPanel.add(enableProxy);
		

	  	final JButton resetProxy = new JButton();
	  	resetProxy.setBounds(202, 184, 81, 19);
	  	resetProxy.setText("Reset");
	  	resetProxy.addActionListener(new ActionListener() {
	  		public void actionPerformed(final ActionEvent e) {
	  			  	
	  			/*System.getProperties().put( "proxySet", "false" );
	  			System.getProperties().put( "proxyHost", "" );
	  			System.getProperties().put( "proxyPort", "");*/
	  			if (enableProxy.isSelected()){
	  				proxy.setText("");
	  				proxyPort.setText("");
	  			}
	  			//JOptionPane.showMessageDialog(null, "Settings saved\n","Proxy Settings",JOptionPane.INFORMATION_MESSAGE);
	  			//popup.dispose();
	  		}
	  	});
	  	proxyPanel.add(resetProxy);
	

	  	final JLabel proxyLabel = new JLabel();
	  	proxyLabel.setBounds(21, 47, 64, 15);
	  	proxyLabel.setText("Address:");
	  	proxyPanel.add(proxyLabel);

	  	final JLabel proxyPortLabel = new JLabel();
	  	proxyPortLabel.setBounds(50, 83, 35, 19);
	  	proxyPortLabel.setText("Port:");
	  	proxyPanel.add(proxyPortLabel);


		final JButton saveSettingButton = new JButton();
		saveSettingButton.setText("Save");
		saveSettingButton.setBounds(385, 355, 105, 25);
		saveSettingButton.addActionListener(new ActionListener() {
	  		public void actionPerformed(final ActionEvent e) {
	  			String localFile=path.getText();
		        try {
					Configuration.setNewConfiguration("repository", "link", localFile);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	  			if (enableProxy.isSelected()){
	  				System.getProperties().put( "proxySet", "true" );
	  				System.getProperties().put( "proxyHost", proxy.getText() );
	  				System.getProperties().put( "proxyPort", proxyPort.getText());
	  				
	  				try {
	  					//ricerca.setNewConfiguration("proxy","address", proxy.getText());
	  					//ricerca.setNewConfiguration("proxy","port", proxyPort.getText());
	  					
	  					Configuration.setNewConfiguration("proxy","address", proxy.getText());
	  					Configuration.setNewConfiguration("proxy","port", proxyPort.getText());
	  					Configuration.setNewConfiguration("proxy","enabled", "yes");
	  					Configuration.getConfiguration().put("proxyAddress",proxy.getText());
	  					Configuration.getConfiguration().put("proxyPort",proxyPort.getText());
	  					Configuration.getConfiguration().put("proxyEnabled","yes");
	  					
	  				} catch (Exception e1) {
	  					// TODO Auto-generated catch block
	  					e1.printStackTrace();
	  				}
	  			}
	  			else{
	  				System.getProperties().put( "proxySet", "false" );
	  				System.getProperties().put( "proxyHost", "" );
	  				System.getProperties().put( "proxyPort", "");
	  				Configuration.getConfiguration().put("proxyAddress","");
  					Configuration.getConfiguration().put("proxyPort","");
  					Configuration.getConfiguration().put("proxyEnabled","no");
	  				try {
						Configuration.setNewConfiguration("proxy","enabled", "no");
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	  			}
	  			JOptionPane.showMessageDialog(null, "Settings saved","Proxy Settings",JOptionPane.INFORMATION_MESSAGE);
	  			//setVisible(false);
	  			dispose();
	  		}

	  		
	  	});
		getContentPane().add(saveSettingButton);
		
		try {
			//Configuration.getConfiguration();
			//settings=Configuration.getAllConfiguration();
			Configuration.findLocalConfiguration();
			settings=Configuration.getConfiguration();
			proxy.setText(settings.get("proxyAddress").toString());
			proxyPort.setText(settings.get("proxyPort").toString());
			if (settings.get("proxyEnabled").toString().equalsIgnoreCase("yes")){
				enableProxy.setSelected(true);
				
			}else{
				enableProxy.setSelected(false);
		    }
			proxy.setEnabled(enableProxy.isSelected());
			proxyPort.setEnabled(enableProxy.isSelected());
			path.setText(settings.get("localRepoLink").toString());
			
			
		/*} catch (SocketTimeoutException e1) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "Time out","Error", JOptionPane.ERROR_MESSAGE);
			
		}*/
		}catch (Configuration.RemoteExc e){
			JOptionPane.showMessageDialog(null, "Incomplete XML remote file configuration ","File config error", JOptionPane.ERROR_MESSAGE);

		} 
		catch (Configuration.LocalExc e){
			JOptionPane.showMessageDialog(null, "Incomplete XML local configuration - ","File config error", JOptionPane.ERROR_MESSAGE);
		}
		catch (Configuration.FileConfigExc e){
			JOptionPane.showMessageDialog(null, "Incomplete XML file configuration","File config error", JOptionPane.ERROR_MESSAGE);

		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "Server error\nError in XML file server configuration","Error", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}

		
	}

	
	
}
